package com.aipii.demo.client;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.ssl.SSLContextBuilder;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;


public class JsonRpcTester {
	private static Logger logger = LoggerFactory.getLogger(JsonRpcTester.class);
	
	@Test
	public void testHttps() throws Throwable {
		JsonRpcHttpClient client = new JsonRpcHttpClient(new URL("https://vicc.chinaamc.com:5002/api/V1"));
//		SSLContext sslContext = null;
//		try {
//			sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//				// 信任所有
//				public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//					return true;
//				}
//			}).build();
//		} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
//			logger.error(e.getMessage(), e);
//		}
//		client.setSslContext(sslContext);
		Map<String, Object> param = new HashMap<>();
		param.put("asset_code_list", new String[]{"006022"});
		param.put("with_benm_data", true);
		param.put("with_strcfund_data", true);
		JSONObject obj = client.invoke("get_asset_info", param, JSONObject.class);
		logger.debug("result:{}", obj);
	}
	
	@Test
	public void testHttps2() throws Throwable {
		
		String result = HttpUtil.post("https://127.0.0.1:8443/get_user_info",new HashMap<String, Object>());
		
		logger.debug("https post response:{}",result);
		
		JsonRpcHttpClient client = new JsonRpcHttpClient(new URL("https://127.0.0.1:8443/rpc/users"));
		
		// setup a Trust Strategy that allows all certificates.
		//
		SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
			@Override
			public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
				return true;
			}
		}).build();
		
		// don't check Hostnames, either.
		//      -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
		HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
		
		client.setSslContext(sslContext);
		client.setHostNameVerifier(hostnameVerifier);
		
		Map<String, Object> param = new HashMap<>();
		param.put("id", 20);
		JSONObject obj = client.invoke("findOne", param, JSONObject.class);
		logger.debug("result:{}", obj);
	}
	
	@Test
	public void testHttps3() throws Throwable {
		JsonRpcHttpClient client = new JsonRpcHttpClient(new URL("https://127.0.0.1:8443/rpc/users"));
		// setup a Trust Strategy that allows all certificates.
		//
		SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
			@Override
			public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
				return true;
			}
		}).build();
		// don't check Hostnames, either.
		//      -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
		HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
		client.setSslContext(sslContext);
		client.setHostNameVerifier(hostnameVerifier);
		
		Map<String, Object> param3 = new HashMap<>();
		param3.put("id", 20);
		JSONObject obj3 = client.invoke("find2", new Object[]{param3}, JSONObject.class);
		logger.debug("result3:{}", obj3);
	}
}
